from bcc import BPF
import argparse
import sys
import subprocess
import os.path
import time
import ctypes as ct
import psutil

#---------------------------args------------------------#
examples=""

parser = argparse.ArgumentParser(
    description="probe irq/preempt off state, related resources and find longest irq/preempt off group",
    formatter_class=argparse.RawDescriptionHelpFormatter,
    epilog=examples)

parser.add_argument("-p", "--preemptoff", action="store_true",
                    help="Probe preemption off")

parser.add_argument("-i", "--irqoff", action="store_true",
                    help="Probe irq off")

parser.add_argument("-d", "--duration", default=500,
                    help="Filter duration below this(us)")


args = parser.parse_args()

preemptoff = False
irqoff = False

if args.irqoff:
    irqoff = True
if args.preemptoff:
    preemptoff = True

# default probe irpoff
if (not args.irqoff) and (not args.preemptoff):
    irqoff = True

# open subprocess to output kernal debugfs path
debugfs_path = subprocess.Popen ("cat /proc/mounts | grep -w debugfs" +
    " | awk '{print $2}'",
    shell=True,
    stdout=subprocess.PIPE).stdout.read().split(b"\n")[0]

# unable to find document
if debugfs_path == "":
    print("ERROR: Unable to find debugfs mount point");
    sys.exit(0)

trace_path = debugfs_path + b"/tracing/events/preemptirq/";

# unable to find tracepoint
if irqoff:
    if (not os.path.exists(trace_path + b"irq_disable") or
        not os.path.exists(trace_path + b"irq_enable")):
        print("ERROR: required tracing point are not available\n" +
        "Make sure the kernel is built with CONFIG_DEBUG_PREEMPT " +
        "CONFIG_PREEMPT_TRACER " +
        "CONFIG_PREEMPTIRQ_TRACEPOINTS"+
        "(CONFIG_PREEMPTIRQ_EVENTS older than kernel 4.19) and CONFIG_TRACE_IRQFLAGS enabled.\n"
        "Also please disable " +
        "CONFIG_PROVE_LOCKING and CONFIG_LOCKDEP on older kernels.")
        
        
if preemptoff:
    if (not os.path.exists(trace_path + b"preempt_disable") or
        not os.path.exists(trace_path + b"preempt_enable")):
        print("ERROR: required tracing point are not available\n" +
        "Make sure the kernel is built with CONFIG_DEBUG_PREEMPT " +
        "CONFIG_PREEMPT_TRACER " +
        "CONFIG_PREEMPTIRQ_TRACEPOINTS"+
        "(CONFIG_PREEMPTIRQ_EVENTS older than kernel 4.19) and TRACE_PREEMPT_TOGGLE enabled.\n"
        "Also please disable " +
        "CONFIG_PROVE_LOCKING and CONFIG_LOCKDEP on older kernels.")


NAME = "gala-spider"
VERSION = "1.0.0"
DESC = "OS Topological Graph Storage Service and Cause Inference Service"
LONG_DESC = """
OS Topological Graph Storage Service and Cause Inference Service
"""

INSTALL_REQUIRES = [
    "requests",
    "pyyaml",
    "pyarango>=2.0.1",
    "kafka-python>=2.0.2",
    "scipy",
    "networkx"
]

setup(
    name=NAME,
    version=VERSION,
    description=DESC,
    author_email="hexiujun1@huawei.com",
    url="https://gitee.com/openeuler/A-Ops/tree/master/gala-spider",
    keywords=["OS Topological Graph Storage", "Cause Inference"],
    install_requires=INSTALL_REQUIRES,
    packages=find_packages(exclude=('tests',)),
    data_files=[
        ('/etc/gala-spider/', ['config/gala-spider.yaml', 'config/topo-relation.yaml', 'config/ext-observe-meta.yaml']),
        ('/etc/gala-inference/', ['config/gala-inference.yaml', 'config/ext-observe-meta.yaml',
                                  'config/infer-rule.yaml']),
        ('/usr/lib/systemd/system/', ['service/gala-spider.service', 'service/gala-inference.service']),
    ],
    entry_points={
        'console_scripts': [
            'spider-storage=spider.storage_daemon:main',
            'gala-inference=cause_inference.__main__:main',
        ]
    },
    long_description=LONG_DESC,
)

setuptools.setup(
    name='patch-tracking',
    version='1.0.0',
    packages=setuptools.find_packages(),
    url='https://openeuler.org/zh/',
    license='Mulan PSL v2',
    author='ChenYanpan',
    author_email='chenyanpan@huawei.com',
    description='This is a tool for automatically tracking upstream repository code patches',
    requires=['requests', 'flask', 'flask_restx', 'Flask_SQLAlchemy', 'Flask_APScheduler'],
    data_files=[
        ('/etc/patch-tracking/', ['patch_tracking/settings.conf']),
        ('/etc/patch-tracking/', ['patch_tracking/logging.conf']),
        ('/var/patch-tracking/', ['patch_tracking/db.sqlite']),
        ('/usr/bin/', ['patch_tracking/cli/patch-tracking-cli']),
        ('/usr/bin/', ['patch_tracking/patch-tracking']),
        ('/usr/bin/', ['patch_tracking/cli/generate_password']),
        ('/usr/lib/systemd/system/', ['patch_tracking/patch-tracking.service']),
    ],
)
#2590    https://gitee.com/openeuler/Preempt_RT.git      Preempt_RT/ probe_tools / probe.py 
#1312    https://gitee.com/openeuler/gala-spider.git              gala-spider/ setup.py 
#980      https://gitee.com/openeuler/patch-tracking.git    patch-tracking/ setup.py
